/**
 * @description:
 * @Author: wuzhenbin
 * @CreateTime: 2025-02-15
 */
public class leetcode_19 {
    public static void main(String[] args) {
        ListNode listNode = new ListNode(1,null);
        System.out.println(removeNthFromEnd(listNode, 1));
    }

    public static ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode headNode = new ListNode(0, head);
        ListNode slow = headNode;
        ListNode fast = headNode;
        int count = n;
        while (count > 0) {
            fast = fast.next;
            if (fast == null) return null;
            count--;
        }
        while (fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next;
        }
        slow.next = slow.next.next;
        return headNode.next;
    }

    public static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }
}
